package com.jy.adscm.operation.base.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.TaskManagerAttchment;
import com.jy.adscm.operation.base.bean.TaskWorkAttachment;
import com.jy.adscm.operation.base.service.TaskWorkAttachmentService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.system.utils.AdscmSystemUtils;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 附件 前端控制器
 * </p>
 *
 * @author zyl
 * @since 2020-07-28
 */
@RestController
@RequestMapping("/adscm/operation/taskWorkAttachment")
public class TaskWorkAttachmentController {
    @Value("${ly.upload.imagePath}")
    private  String imagePath;
    @Value("${ly.upload.imageUrl}")
    private  String imageUrl;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    TaskWorkAttachmentService taskWorkAttachmentService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody   TaskWorkAttachment taskWorkAttachment,  HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        taskWorkAttachment.setCreator(token.getUser().getId().toString());
        taskWorkAttachment.setCreateTime(new Date());
        if (AdscmBaseUtils.getInstance().isNotEmpty(taskWorkAttachment.getPath(),null)){
            String[] split = taskWorkAttachment.getPath().split("/");
            taskWorkAttachment.setName(split[split.length-1]);
            taskWorkAttachment.setPath(taskWorkAttachment.getPath().replace(imageUrl,""));
        }
        boolean insert = taskWorkAttachmentService.insert(taskWorkAttachment);
        return new SimpleCoreResponse("200","添加成功",insert);
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable  Integer id,  HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = taskWorkAttachmentService.deleteTaskWorkAttachment(id,token);
        return new SimpleCoreResponse("200","删除成功",insert);
    }
    @RequestMapping("selectByPid/{pid}")
    public SimpleCoreResponse selectByPid(@PathVariable  Integer pid,  HttpServletRequest request, HttpServletResponse response)throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<TaskWorkAttachment> taskWorkAttachments= taskWorkAttachmentService.selectTaskWorkAttachment(pid,token);
        return new SimpleCoreResponse("200","删除成功",taskWorkAttachments);
    }

}

